//
//  SLMBPDemoController.m
//  Objective-C
//
//  Created by S_Line on 2018/6/19.
//  Copyright © 2018年 S_Line. All rights reserved.
//

#import "SLMBPDemoController.h"

#import "MBProgressHUD+Factory.h"

#define showProgress @"展示MPB"
#define showToastText @"展示ToastText"
#define showToastTextImage @"展示ToastText和Image"

@interface SLMBPDemoController ()

@property (nonatomic, weak) UIScrollView *scrollView;

@property (nonatomic, weak) UIView *lastButton;

@end

@implementation SLMBPDemoController

- (void)initNavBar {
    self.navTitle = @"MBP示范";
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initNavBar];
    
    [self initView];
}

- (void)initView {
    UIScrollView *scrollView = [UIScrollView new];
    self.scrollView = scrollView;
    [self.view addSubview:scrollView];
    [scrollView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.top.mas_equalTo(kNavigationBarHeight);
        make.bottom.mas_equalTo(-kSafeBottomMargin);
    }];
    
    UIView *widthView = [UIView new];
    [scrollView addSubview:widthView];
    [widthView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.height.mas_equalTo(0);
        make.width.equalTo(scrollView.mas_width).mas_offset(0);
    }];
    
    [self renderClickViewWithTitle:showProgress];
    [self renderClickViewWithTitle:showToastText];
    [self renderClickViewWithTitle:showToastTextImage];
    
    UIView *endView = [UIView new];
    [scrollView addSubview:endView];
    [endView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(0);
        make.top.equalTo(self.lastButton.mas_bottom).mas_offset(15);
    }];
}

- (void)renderClickViewWithTitle:(NSString *)title {
    UIButton *button = [UIButton new];
    button.titleLabel.font = [UIFont boldSystemFontOfSize:13];
    [button setTitle:title forState:UIControlStateNormal];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [button setBackgroundImage:[UIImage imageWithColor:[UIColor lightGrayColor]] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.scrollView addSubview: button];
    [button mas_remakeConstraints:^(MASConstraintMaker *make) {
        if (self.lastButton) {
            make.top.equalTo(self.lastButton.mas_bottom).mas_offset(8);
        } else {
            make.top.mas_equalTo(15);
        }
        
        make.left.mas_equalTo(30);
        make.right.mas_equalTo(-30);
        make.height.mas_equalTo(30);
    }];
    
    self.lastButton = button;
}

- (void)buttonClick:(UIButton *)sender {
    if ([sender.currentTitle isEqualToString:showProgress]) {
        MBProgressHUD *hud = [MBProgressHUD showDarkHUDAddedTo:self.view animated:true];
        
        [hud hideAnimated:true afterDelay:3];
    } else if ([sender.currentTitle isEqualToString:showToastText]) {
        MBProgressHUD *hud = [MBProgressHUD showToast:@"展示Toast" image:nil];
        
        [hud hideAnimated:true afterDelay:3];
    } else if ([sender.currentTitle isEqualToString:showToastTextImage]) {
        MBProgressHUD *hud = [MBProgressHUD showToast:@"展示Toast" image:[UIImage imageNamed:@"nav_back"]];
        
        [hud hideAnimated:true afterDelay:3];
    }
}

@end
